package org.leon;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class AppMain {

    public static void main(String[] args) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181",retryPolicy);
        client.start();

        final NodeCache nodeCache =
                new NodeCache(client, "/workerPath", false);
        NodeCacheListener l = new NodeCacheListener() {

            public void nodeChanged() throws Exception {
                ChildData childData = nodeCache.getCurrentData();
                System.out.println("ZNode节点状态改变, path=" + childData.getPath());
                System.out.println("ZNode节点状态改变, data=" + new String(childData.getData(), "Utf-8"));
                System.out.println("ZNode节点状态改变, stat=" + childData.getStat());
            }
        };
        nodeCache.getListenable().addListener(l);
        nodeCache.start();

    }
}
